package leetcode.code2226;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int maximumCandies(int[] candies, long k) {
		long sum = 0;// 一共多少糖
		int max = 0;// 最大一堆儿多少糖
		for (int i = 0; i < candies.length; i++) {
			int num = candies[i];
			sum += num;
			max = Math.max(max, num);
		}
		if (sum < k) {
			return 0;
		}
		int l = 1, r = max;
		int ans = 0;
		while (l <= r) {
			int m = ((r - l) >> 1) + l;
			long cnt = 0;
			for (int i = 0; i < candies.length; i++) {
				cnt += candies[i] / m;// 按照每个小孩m个糖，可以分多少个人
			}
			if (cnt >= k) {// 能多分出人，说明每个小孩的糖可能分少了，尝试多分一个
				ans = m;
				l = m + 1;
			} else {
				r = m - 1;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
